% script de perceptron sur 500 échantillon aléatoires

load('data.mat')

%% tirage de 500 échantillons/

n = 100;
c1 = rgb1;
c2 = rgb2;
ct = rgb2_t;

clc;
s1 = size(c1);
randomInt1 = drawRandom(n,1,s1(1));

s2 = size(rgb2);
randomInt2 = drawRandom(n,1,s2(1));

s3 = size(ct);
randomInt3 = drawRandom(length(ct),1,s3(1));

randCls1 = c1(randomInt1,:);
%randCls1 = randCls1(:,2:3);
randCls2 = c2(randomInt2,:);
%randCls2 = randCls2(:,2:3);

randTest = ct(randomInt3,:);
%randTest = randTest(:,2:3);

fprintf('tirage aléatoire\t--OK--\n');



%% perceptron /

clc;
w = mPerceptron(randCls1, randCls2, 1000)
if w==-1
    return;
end

w = w(2:end);

% classification
sum = 0;
for i=1:length(randTest)
    if w'*randTest(i,:)' > 0
        sum = sum+1;
    end
end

fprintf('erreur de classification\t%f\n', sum/length(randTest));





%% moindre carres /

clc;
w = mLeastSquares(randCls1, randCls2, 1000);
if w==-1
    return;
end

w = w(2:end);

% classification
sum = 0;
for i=1:length(randTest)
    if w'*randTest(i,:)' > 0
        sum = sum+1;
    end
end
plot(randTest(:,1),randTest(:,2),randTest(:,3));
fprintf('erreur de classification\t%f\n', sum/length(randTest));
%% k-Nearest Neighbors /

   clc;
   c1 = rgb1(:,2:3);
   c2 = rgb3(:,2:3);
   ct = rgb1_t(:,2:3);

   trn=0;
   trn.X = horzcat(c1',c2');
   trn.y = horzcat(ones(1,length(c1)),ones(1,length(c2))+1);
   trn.dim = 2;
   trn.num_data = length(c1)+length(c2);
   
   tst=0;
   tst.X = ct';
   tst.y = ones(1,length(ct))+1;
   tst.dim = 2;
   tst.num_data = length(ct);
   %tic;
   res = [0 0 0]
   for k=1:2:31
        tic;
       model = knnrule(trn,k);
       ypred = knnclass(tst.X,model);
       res = vertcat(res, [k cerror( ypred, tst.y ) toc])
       %fprintf('\nnombre voisins\t\t\t%d\n',k);
       %fprintf('temps de calcul\t\t\t%f\n', toc);
       %fprintf('Erreur de classification\t%f\n',cerror( ypred, tst.y ));
   end
%    tmp = 'traçage en cours\t\t...\n';
% 
%    figure;
%    fprintf(tmp);
%    ppatterns(trn);
%    ppatterns(ct');
%    pboundary(model);
%    tmp = 'fin traçage\t\t\t--OK--\n';
%    fprintf(tmp);
   fprintf('k-Nearest Neighbors\t\t--OK--\n');
%% test histo
X = res(:,1);
class = res(:,2);
time = res(:,3);

plot(res(:,1), res(:,2)*100);
figure
plot(res(:,1), res(:,3));

%figure;
%hist(time, X);


   
   
   
   %% Support Vectors Machines /
   
   clc;
   fprintf('Support Vector Machines\n')
   %trn =0; tst=0;
   trn.X = horzcat(randCls1', randCls2');
   trn.y = horzcat(ones(1,length(randCls1)),ones(1,length(randCls2))+1);
   trn.dim = 3;
   trn.num_data = length(trn.y);
   tic;
   model = smo(trn, struct('ker', 'rbf', 'arg',10,'C',200));
   toc;
   
   tst=0;
   tst.X = randTest';
   tst.y = ones(1,length(randTest))+1;
   tst.dim = 2;
   tst.num_data = length(randTest);
   ypred = svmclass( tst.X, model );
   cerror( ypred, tst.y )
   
   figure; 
   ppatterns(trn); 
   psvm(model);
   
   %% Moindre carré